package adapter;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.administrator.car.R;
import com.example.administrator.car.WoYaoYouHuiActivity;
import com.example.administrator.car.YouHuiActivity;

import java.util.List;

import bean.CarYouHui;

/**
 * Created by Administrator on 2015/4/21.
 */
public class YouHuiAdapter extends BaseAdapter {
    private List<CarYouHui> list;
    private Context context;
    private ViewHolder holder;
    private String web_url;
    private String modelId;
    private String dealerNewsId;
    private String area;
    private String image;

    public YouHuiAdapter(List<CarYouHui> list, Context context){
        this.context=context;
        this.list=list;
    }
    @Override
    public int getCount() {
        if (list!=null){
           // Log.i("123","----->"+list.size());
            return list.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        holder = new ViewHolder();
        if (convertView==null){
           convertView= LayoutInflater.from(context).inflate(R.layout.list_item_youhui,null,false);

            holder.carName= (TextView) convertView.findViewById(R.id.car_name);
            holder.carcomp= (TextView) convertView.findViewById(R.id.car_company);
            holder.carPrice= (TextView) convertView.findViewById(R.id.car_price);
            holder.carDownPrice= (TextView) convertView.findViewById(R.id.car_price_down);
            holder.phone= (TextView) convertView.findViewById(R.id.car_phone);
            holder.youHui= (TextView) convertView.findViewById(R.id.car_woyaoyouhui);
            holder.item_layout= (LinearLayout) convertView.findViewById(R.id.item_layout_youhui);
            convertView.setTag(holder);


        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        //数据
        dealerNewsId = list.get(position).getDealerNewsId();
        modelId = list.get(position).getModelId();
        web_url = "http://mrobot.pcauto.com.cn/v3/price/promotionDetailv45/"
                + dealerNewsId + "?modelId=" + modelId + "&type=0";
        area=list.get(position).getArea();
        image=list.get(position).getImg();
        CarYouHui car=list.get(position);
        holder.carName.setText(car.getCarName());
        holder.carcomp.setText(car.getCompName());
        holder.carPrice.setText("￥"+car.getPrice()+"万");
        holder.carDownPrice.setText( "￥"+car.getDownPrice()+"万");
        holder.phone.setText("电话咨询");
        holder.youHui.setText("我要优惠");


        holder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, YouHuiActivity.class);
                intent.putExtra("modelId",modelId);
                intent.putExtra("web_url",web_url);
                context.startActivity(intent);
            }
        });

        holder.youHui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, WoYaoYouHuiActivity.class);
                intent.putExtra("area",area);
                intent.putExtra("image",image);
                context.startActivity(intent);
            }
        });
        return convertView;
    }



    class ViewHolder{
        TextView carName;
        TextView carcomp;
        TextView carPrice;
        TextView carDownPrice;
        TextView phone;
        TextView youHui;
        LinearLayout item_layout;
    }
}
